package com.service.sys.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.config.globalException.BusinessException;
import com.entity.sys.PoliticsStatus;
import com.entity.sys.User;
import com.mapper.sys.PoliticsStatusMapper;
import com.service.sys.PoliticsStatusService;
import com.service.sys.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author clay
 * @since 2022-02-23
 */
@Service
public class PoliticsStatusServiceImpl extends ServiceImpl<PoliticsStatusMapper, PoliticsStatus> implements PoliticsStatusService {

    @Resource
    private UserService userService;

    @Resource
    private PoliticsStatusMapper politicsStatusMapper;
    @Override
    public Boolean deleteById(String id) {
        List<User> users = userService.list(new QueryWrapper<User>().eq(
                "department_id", id
        ));
        if (!CollectionUtil.isEmpty(users)) {
            throw new BusinessException("存在被引用的政治面貌信息, 无法删除!");
        }
        return this.removeById(id);
    }

    @Override
    public List<PoliticsStatus> select(PoliticsStatus politicsStatus) {
        QueryWrapper<PoliticsStatus> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(politicsStatus.getName())) {
            wrapper.eq(
                    "name", politicsStatus.getName()
            );
        }
        if (Objects.nonNull(politicsStatus.getEnabled())) {
            wrapper.eq(
                    "enabled", politicsStatus.getEnabled()
            );
        }
        return politicsStatusMapper.selectList(wrapper);
    }
}
